import cartList from "../cart-list/cart-list.vue";

import {
	request,
	debounce
} from "@/common/js/util.js";

export default {
	name: 'ShoppingCart',
	components: {
		cartList
	},
	props: {

	},
	data() {
		return {
			isCanOrder: true, // 是否可以下单
			isShowCart: false, // 是否显示购物车
			isMeetOut: false, // 是否满足外卖起送条件
		};
	},
	computed: {
		// 就餐方式
		diningMode() {
			return this.$store.state.diningMode;
		},
		// 购物车列表
		cartList() {
			return this.$store.state.cartList;
		},
		// 购物车数量
		cartTotalNum() {
			const cartTotalNum = this.$store.state.cartTotalNum;
			if (!cartTotalNum) {
				this.isShowCart = false;
			}
			return cartTotalNum;
		},
		// 订单数据
		order() {
			return this.$store.state.order;
		},
		// 当前门店信息
		currentStoreInfo() {
			return this.$store.state.currentStoreInfo;
		}
	},
	watch: {
		'order.cartProductFeeTotal'() {
			this.judgeIsMeetOut();
		}
	},
	mounted() {
		this.judgeIsMeetOut();

		// 门店是否可以下单判断
		const {
			preBook,
			openingCategory
		} = this.currentStoreInfo;
		
		if(!openingCategory) return;

		if (openingCategory === 'opening') {
			this.isCanOrder = true;
		} else if (openingCategory === 'beforeOpening') {
			if (preBook === '1') {
				this.isCanOrder = true;
			} else {
				uni.showToast({
					title: '门店还未营业',
					icon: 'none',
					duration: 3000
				})
				this.isCanOrder = false;
			}
		} else {
			uni.showToast({
				title: '门店已打烊',
				icon: 'none',
				duration: 3000
			})
			this.isCanOrder = false;
		}

	},
	methods: {
		// 判断是否满足外卖条件
		judgeIsMeetOut() {
			const startFee = this.currentStoreInfo.start_fee || 0;
			const curFee = this.order.cartProductFeeTotal || 0;
			if (curFee >= startFee) {
				this.isMeetOut = true;
			} else {
				this.isMeetOut = false;
			}
		},

		// 显示购物车
		onShowCartList() {
			if (this.cartTotalNum) {
				this.isShowCart = !this.isShowCart;
			}
		},

		// 隐藏购物车
		onHideCartList() {
			this.isShowCart = false;
		},

		//清空购物车
		clearCartList() {
			const that = this;
			uni.showModal({
				title: '提示',
				content: '确定要清空购物车吗？',
				confirmColor: '#FFA200',
				success: function(res) {
					if (res.confirm) {
						that.$store.commit('clearCartList', 'yes')
					}
				}
			});
		},

		// 点击结算按钮
		onTapBtn() {
			this.$emit("onTapBtn", '');
		}
	}
};
